package i2.application.checklist.dao.jpa.impl;

import i2.application.checklist.dao.ApplicationDao;
import i2.application.checklist.entities.Application;
import i2.application.fmw.dao.jpa.JPADAO;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

@Repository("applicationDao")
public class ApplicationDaoImpl extends JPADAO<Application> implements ApplicationDao {

//	private static final Logger logger = LoggerFactory.getLogger(ApplicationDaoImpl.class);
	
	
	/* (non-Javadoc)
	 * @see i2.application.checklist.dao.ApplicationDao#isNameNotUsed(i2.application.checklist.entities.Application)
	 */
	@Override
	public boolean isNameAlreadyUsed(Application application) {
		Query query = entityManager.createNamedQuery("Application.isNameAlreadyUsed");
		query.setParameter("nom", application.getNom());
		query.setParameter("id", application.getId());
		
		Object isUnique = null;
		try {
			isUnique = query.getSingleResult();
			logger.debug("le nom existe déjà ? : {}",isUnique);
		} catch (NoResultException e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean hasLivraisons(Application application) {
		Query query = entityManager.createNamedQuery("Application.hasLivraisons");
		query.setParameter("id", application.getId());
		Object hasLivraisons = null;
		try {
			hasLivraisons = query.getSingleResult();
			logger.debug("l'application a des livraisons ? : {}", hasLivraisons);
		} catch (NoResultException e) {
			return false;
		}
		return true;
	}

}
